package com.lzw.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;

/**
 * @auther: lzw
 * @date: 2021/9/27 - 09 - 27 - 16:28
 * @Description: token的生成和解析类
 */

public class JwtUtils {
    //确定密钥
    private static final String jwtToken = "C56&nI@3*WtOPpFt";
    //生成token
    public static String createToken(String rid){
        return JWT.create()
                .withAudience(rid)
                .withIssuedAt(new Date(System.currentTimeMillis()))
                .withExpiresAt(new Date(System.currentTimeMillis()+24*60*60*1000))
                .sign(Algorithm.HMAC256(jwtToken));
    }

    public static Integer vaildToken(String token){
        return  new Integer(JWT.decode(token).getAudience().get(0));
    }

    public static void main(String[] args) {
        String token = JwtUtils.createToken("1");
        Integer integer = JwtUtils.vaildToken(token);
        System.out.println(integer);
    }
}
